import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NetScrollHomePage extends StatefulWidget {
  const NetScrollHomePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return ScrollHomePageState();
  }
}

class ScrollHomePageState extends State with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(" 配制"),
      ),

      /// 处理滑动
      body: buildNestedScrollView(),
    );
  }

  ///构建滑动布局
  NestedScrollView buildNestedScrollView() {
    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool b) {
        return [
          SliverAppBar(
            ///true SliverAppBar 不会滑动
            pinned: true,

            ///是否随着滑动隐藏标题
            floating: true,

            ///SliverAppBar展开的高度
            expandedHeight: 200,
            flexibleSpace: buildFlexibleSpaceBar(),
            bottom: buildTabBar(),
          ),
        ];
      },

      ///主体部分
      body: buildTabBarView(),
    );
  }

  late TabController tabController;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 3, vsync: this);
  }

  TabBarView buildTabBarView() {
    return TabBarView(
      controller: tabController,
      children: <Widget>[
        SingleChildScrollView(
          child: Container(
            alignment: Alignment.bottomLeft,
            height: 1000,
            child: Text("这是第一个页面"),
          ),
        ),
        const Text(
          "这是第二个页面",
          style: TextStyle(color: Colors.blue),
        ),
        const Text(
          "这是第三个页面",
          style: TextStyle(color: Colors.red),
        ),
      ],
    );
  }

  TabBar buildTabBar() {
    return TabBar(
      controller: tabController,
      tabs: <Widget>[
        const Tab(
          text: "标签一",
        ),
        const Tab(
          text: "标签二",
        ),
        const Tab(
          text: "标签三",
        ),
      ],
    );
  }

  String imageUrl =
      "https://d.ifengimg.com/w480_h270_q90_webp/x0.ifengimg.com/ucms/2023_42/BCE50FE08CEAF6863A83175DA4C405B9375ACCC7_size76_w975_h549.jpg";

  FlexibleSpaceBar buildFlexibleSpaceBar() {
    return FlexibleSpaceBar(
//                title: Text("FlexibleSpaceBar title"),
      centerTitle: true,
      background: Container(
        color: Colors.blue[300],
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            SizedBox(
              height: 150,
              child: Image.network(
                imageUrl,
                fit: BoxFit.fill,
                height: 160,
                width: 400,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
